home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE10 / SCROLLDC.C < prev    next >
C/C++ Source or Header  |  1996-01-29  |  7KB  |  239 lines

  1.  
  2. #include <windows.h>  
  3. #include "ScrollDC.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. VOID Scroll( HWND hWnd, int* pnCurPos, WORD wScroll )
  108. {
  109.    int nMin, nMax;
  110.    int nPos;
  111.  
  112.    // Retrieve the scroll information of the scroll bar.
  113.    //...................................................
  114.    GetScrollRange( hWnd, SB_HORZ, &nMin, &nMax );
  115.    nPos = GetScrollPos( hWnd, SB_HORZ );
  116.  
  117.    switch( wScroll )
  118.    {
  119.       // Scroll to the right.
  120.       //.....................
  121.       case SB_LINERIGHT :
  122.              if ( *pnCurPos < nMax )
  123.                *pnCurPos += 1;
  124.              break;
  125.  
  126.       // Scroll to the left.
  127.       //....................
  128.       case SB_LINELEFT :
  129.              if ( *pnCurPos > 0 )
  130.                 *pnCurPos -= 1;
  131.              break;
  132.    }
  133.  
  134.    // If the position has changed,
  135.    // actually scroll the window.
  136.    //.............................
  137.    if ( nPos != *pnCurPos )
  138.    {
  139.       RECT cltRect;
  140.       HDC  hDC  = GetDC( hWnd );
  141.  
  142.       GetClientRect( hWnd, &cltRect );
  143.       cltRect.left   += 20;
  144.       cltRect.right  -= 20;
  145.       cltRect.top    += 20;
  146.       cltRect.bottom -= 20;
  147.  
  148.       ScrollDC( hDC, (*pnCurPos-nPos)*20, 0, NULL, &cltRect, 
  149.                 NULL, NULL );
  150.  
  151.       SetScrollPos( hWnd, SB_HORZ, *pnCurPos, TRUE );
  152.    }
  153. }
  154.  
  155.  
  156. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  157. {
  158. static int  nCurPos   = 0;
  159.  
  160.    switch( uMsg )
  161.    {
  162.       case WM_CREATE :
  163.               ShowScrollBar( hWnd, SB_HORZ, FALSE );
  164.               SetScrollRange( hWnd, SB_HORZ, 0, 10, FALSE );
  165.               SetScrollPos( hWnd, SB_HORZ, 0, TRUE );
  166.               break;
  167.  
  168.       case WM_HSCROLL :
  169.               Scroll( hWnd, &nCurPos, LOWORD( wParam ) );
  170.               break;
  171.  
  172.       case WM_COMMAND :
  173.               switch( LOWORD( wParam ) )
  174.               {
  175.                  case IDM_TEST :
  176.                         {
  177.                            HDC  hDC  = GetDC( hWnd );
  178.                            RECT rect;
  179.                            int  i;
  180.  
  181.                            // Paint vertical lines in client area.
  182.                            //.....................................
  183.                            GetClientRect( hWnd, &rect );
  184.  
  185.                            for( i=0; i<20; i++ )
  186.                            {
  187.                               MoveToEx( hDC, i*8, 0, NULL );
  188.                               LineTo( hDC, i*8, rect.bottom );
  189.                            }
  190.  
  191.                            ReleaseDC( hWnd, hDC );
  192.                         }
  193.                         break;
  194.  
  195.                  case IDM_ABOUT :
  196.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  197.                         break;
  198.  
  199.                  case IDM_EXIT :
  200.                         DestroyWindow( hWnd );
  201.                         break;
  202.               }
  203.               break;
  204.       
  205.       case WM_DESTROY :
  206.               PostQuitMessage(0);
  207.               break;
  208.  
  209.       default :
  210.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  211.    }
  212.  
  213.    return( 0L );
  214. }
  215.  
  216.  
  217. LRESULT CALLBACK About( HWND hDlg,           
  218.                         UINT message,        
  219.                         WPARAM wParam,       
  220.                         LPARAM lParam)
  221. {
  222.    switch (message) 
  223.    {
  224.        case WM_INITDIALOG: 
  225.                return (TRUE);
  226.  
  227.        case WM_COMMAND:                              
  228.                if (   LOWORD(wParam) == IDOK         
  229.                    || LOWORD(wParam) == IDCANCEL)    
  230.                {
  231.                        EndDialog(hDlg, TRUE);        
  232.                        return (TRUE);
  233.                }
  234.                break;
  235.    }
  236.  
  237.    return (FALSE); 
  238. }
  239.